/*
  <file> ulc2xml.c </file>
  <brief>
    Generating xml-version of ulc-file reading the structure from it.
    This file is part of ULS, Unified Lexical Scheme.
  </brief>
  <author>
    Stanley J. Hong <babel96@gmail.com>, 2011.
  </author>
*/

#include "ulc2xml.h"
#include "globals.h"

ulc_file_t ulc_file;

ulc_wordlist_t*
new_wordlist_element(char *text)
{
	ulc_wordlist_t *e;

	e = (ulc_wordlist_t *) malloc(sizeof(ulc_wordlist_t));
	if (e == NULL)
		err_panic("malloc error!");
	
	e->wordtext = text;
	e->next = NULL;
	
	return e;
}

void
release_wordlist(ulc_wordlist_t* wrdlst)
{
	ulc_wordlist_t *e, *e_next;

	for (e=wrdlst; e!=NULL; e=e_next) {
		e_next = e->next;
		free(e->wordtext);
		free(e);
	}
}

ulc_token_def_t*
new_token_def(char *name, char* keyw, int tok_id)
{
	ulc_token_def_t*  e;

	if (ulc_file.n_tokdef_list >= ulc_file.n_alloc_tokdef_list) {
		ulc_file.n_alloc_tokdef_list += 128;
		ulc_file.tokdef_list = (ulc_token_def_t*) realloc(ulc_file.tokdef_list,
			ulc_file.n_alloc_tokdef_list * sizeof(ulc_token_def_t));
	}

	e = ulc_file.tokdef_list + ulc_file.n_tokdef_list;

	e->tok_id = tok_id;

	strncpy(e->name, name, ULS_LEXSTR_MAXSIZ);
	e->name[ULS_LEXSTR_MAXSIZ] = '\0';

	strncpy(e->keyword, keyw, ULS_LEXSTR_MAXSIZ);
	e->keyword[ULS_LEXSTR_MAXSIZ] = '\0';

	++ulc_file.n_tokdef_list;
	return e;
}

static const char*
onoff_str(int a)
{
	return a!=0 ? "on" : "off";	
}

void dump_ulc_file(ulc_file_t *ulc, FILE* fout)
{
	ulc_commtype_t   *cmt;
	ulc_quotetype_t  *qmt;
	ulc_id_range_t   *id_range;
	ulc_token_def_t  *tdef;
	int i;

	uls_sysprn_open(fout, NULL);

	uls_print("<ulc-file>\n");
	uls_print("  <file-version>%s</file-version>\n", ulc->filever);
	uls_print("  <ulc-header>\n");
	uls_print("    <version>%s</version>\n", ulc->ver);
	uls_print("    <ulc-attr name=\"want_eof_tok\">%s</ulc-attr>\n", onoff_str(ulc->flags & ULC2XML_WANT_EOF_TOK));
	uls_print("    <ulc-attr name=\"case_insensitive\">%s</ulc-attr>\n", onoff_str(ulc->flags & ULC2XML_CASE_INSENSITIVE));
	uls_print("    <ulc-attr name=\"want_lf_char\">%s</ulc-attr>\n", onoff_str(ulc->flags & ULC2XML_WANT_LF_CHAR));
	uls_print("    <ulc-attr name=\"want_tab_char\">%s</ulc-attr>\n", onoff_str(ulc->flags & ULC2XML_WANT_TAB_CHAR));

	for (i=0; i<ulc_file.n_commtypes; i++) {
		cmt = ulc_file.commtypes + i;
		uls_print("    <comment-mark>\n");
		uls_print("       <parm name=\"start_str\">%s</parm>\n", cmt->start_mark);
		uls_print("       <parm name=\"end_str\">%s</parm>\n", cmt->end_mark);
		uls_print("    </comment-mark>\n");
	}

	for (i=0; i<ulc_file.n_quotetypes; i++) {
		qmt = ulc_file.quotetypes + i;
		uls_print("    <quote-mark>\n");
		uls_print("       <parm name=\"start_str\">%s</parm>\n", qmt->start_mark);
		uls_print("       <parm name=\"end_str\">%s</parm>\n", qmt->end_mark);
		uls_print("    </quote-mark>\n");
	}

	uls_print("    <idfirst_charsets>\n");
	for (i=0; i<ulc_file.n_idfirst_charsets; i++) {
		id_range = ulc_file.idfirst_charsets + i;
		uls_print("      <charset>%s</charset>\n", id_range->charset);
	}
	uls_print("    </idfirst_charsets>\n");

	uls_print("    <id_charsets>\n");
	for (i=0; i<ulc_file.n_id_charsets; i++) {
		id_range = ulc_file.id_charsets + i;
		uls_print("      <charset>%s</charset>\n", id_range->charset);
	}
	uls_print("    </id_charsets>\n");

	uls_print("  </ulc-header>\n");

	uls_print("  <ulc-body>\n");

	for (i=0; i<ulc_file.n_tokdef_list; i++) {
		tdef = ulc_file.tokdef_list + i;
		uls_print("    <tokdef>\n");
		uls_print("      <tok-id>%d</tok-id>\n", tdef->tok_id);
		uls_print("      <tok-name>%s</tok-name>\n", tdef->name);
		uls_print("      <keyword>%s</keyword>\n", tdef->keyword);
		uls_print("    </tokdef>\n");
	}

	uls_print("  </ulc-body>\n");
	uls_print("</ulc-file>\n");

	uls_sysprn_close();
}
